#include "HttpServer.hpp"

#define WWWROOT "./wwwroot/"


void Login(const HttpRequest& req, HttpResponse* resp){
    //1.这里显示一下文本即可
    std::stringstream ss;
    ss << "Your Request: " << "\r\n";
    ss << req._method << " " << req._url << " " << req._version << "\r\n";
    for(auto& it : req._params){
        ss << it.first << ": " << it.second << "\r\n";
    }
    for(auto& it : req._headers){
        ss << it.first << ": " << it.second << "\r\n";
    }
    ss << "\r\n";
    ss << req._text << "\r\n";

    ss << "Login Success" << "\r\n";
    resp->SetContent(ss.str(), "text/plain");
}

void Register(HttpRequest& req, HttpResponse* resp){
    std::string usrname = req.GetParams("username");
    std::string passwd = req.GetParams("password");
    resp->SetContent("Register Success!", "text/plain");
}

void Hello(HttpRequest& req, HttpResponse* resp){
    resp->SetContent("Hello!", "text/plain");
    sleep(15);
}

void PutFile(HttpRequest& req, HttpResponse* resp){
    std::string pathname = WWWROOT + req._url;
    Util::WriteIntoFile(pathname, req._text); 
}

int main(){
    HttpServer svr(8080, 10);
    svr.SetThreadNum(3);
    svr.SetBaseRootDir(WWWROOT);

    /*我自己放的网页：login是post方法，register是get方法*/
    /*但是要是直接在浏览器搜索框那里输入，默认是get*/
    svr.PostRouteSet("/login", Login);
    svr.GetRouteSet("/login", Login);
    svr.GetRouteSet("/register", Register);
    svr.PostRouteSet("/register", Register);
    svr.GetRouteSet("/hello", Hello);
    svr.PutRouteSet("/1234.txt", PutFile);


    svr.Listen();

    return 0;
}